The remaining objects discussed in this article are
database objects, and they are located in folder structures under each
individual database (with the exception of logon triggers). As you expand a database, you will
encounter the Tables folder. A table is the most important object in the
database because it allows you to store data in a logical, structured
manner that can be retrieved and processed as needed. You can create up
to 2 billion tables per database, and each table can have up to 30,000
columns. The number of rows each table can have is only limited by the
available disk space on the server.
1. Default Constraints
You can define
default constraints on columns within a table in order to insert a
predefined value into those columns whenever a specific value is not
provided. Each column can have only one default constraint. You can
create the default when you create a table, or you can add it later
using the ALTER TABLE statement. You cannot alter a default. To "change" a default, you must drop the existing constraint and create a new one.
To create a default on a column when you create a table, you can specify the constraint following the column definition. In Listing 1, we are creating a table called Orders with an OrderID and an OrderDate column. The OrderDate column has a default named DF_Orders_OrderDate. The column will be given the value of getdate() whenever a value is not otherwise specified.
Example 1. Syntax to Create a Default on a Column When Creating a Table
USE AdventureWorks2008 GO
CREATE TABLE Orders (OrderID int identity, OrderDate DateTime NOT NULL CONSTRAINT [DF_Orders_OrderDate] DEFAULT (getdate()) )
|
If you want to add a default to a column in a table that already exists, you can use the ALTER TABLE statement, as shown in Listing 2. You will end up with the exact same table by running the code in Listing 9-5 or Listing 9-6.
Example 2. Syntax to Create a Constraint on a Preexisting Column
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
--Create the Orders table CREATE TABLE Orders (OrderID int identity , OrderDate DateTime NOT NULL)
--Alter the Orders table to add the default constraint ALTER TABLE dbo.Orders ADD CONSTRAINT [DF_Orders_OrderDate] DEFAULT (getdate()) FOR OrderDate
|
You can also use the ALTER TABLE statement to add a new column with a default value, as shown in Listing 3. You can use the WITH VALUES clause to apply the default to any existing row in the table. If the WITH VALUES clause is not used, all of the preexisting rows will contain a NULL value, and the default will only be applied to new rows.
Example 3. Syntax to Add a Column to a Table with a Default Using the ADD VALUES Clause
USE AdventureWorks2008 GO
--Drop the table if it currently exists IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
--Create the Orders table CREATE TABLE Orders (OrderID int NOT NULL)
--Insert 3 records into the Orders table INSERT INTO Orders (OrderID) VALUES(1),(2),(3)
--Alter the table to add the default ALTER TABLE dbo.Orders ADD OrderDate Datetime NULL CONSTRAINT [DF_Orders_OrderDate] DEFAULT (getdate()) WITH VALUES
--Select to see the default was applied to the existing rows SELECT OrderID, OrderDate FROM Orders
|
As you can see in Figure 1, the new default was applied to all three existing columns, giving each row the exact same OrderDate. If you remove the WITH VALUES clause from Listing 3, all of the order dates would be NULL. If you define the new OrderDate column as NOT NULL, you do not have to specify the WITH VALUES clause as it will be implied.
You can drop a default constraint by using the ALTER TABLE statement with the DROP CONSTRAINT clause, as shown in Listing 4.
Example 4. Code Used to Drop a Default Constraint
ALTER TABLE Orders DROP CONSTRAINT DF_Orders_OrderDate